package com.example.greenshuttle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import com.example.greenshuttle.fragment.HistoryFragment;
import com.example.greenshuttle.fragment.HomeFragment;
import com.example.greenshuttle.fragment.SettingFragment;


import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.extras.SoundPullEventListener;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Matrix;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.format.DateUtils;
import android.util.DisplayMetrics;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewParent;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

/**
 * 
 */
public class MainOrder extends FragmentActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {

	public static MainOrder instance = null;
//////////////////////////////////////////////////////////
	//3，fragment
	private Fragment homeFragment = new HomeFragment();
	private Fragment historyFragment = new HistoryFragment();
	private Fragment settingFragment = new SettingFragment();
	//3，View
    private View homeLayout;
    private View historyLayout;
    private View settingLayout;
    //3，ImageView
    private ImageView homeImage;
    private ImageView historyImage;
    private ImageView settingImage;
    
    //滑动的承载者
    private ViewPager viewPager;
    
    private TabPagerAdapter mPagerAdapter;
	


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_order);
		// 启动activity时不自动弹出软键盘
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		instance = this;

		initView();
		
		final ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
		fragmentList.add(homeFragment);
		fragmentList.add(historyFragment);
		fragmentList.add(settingFragment);
		
		mPagerAdapter = new TabPagerAdapter(getSupportFragmentManager(), fragmentList);

		viewPager.setAdapter(mPagerAdapter);
		viewPager.setOnPageChangeListener((OnPageChangeListener) this);

		setTabSelection(0);
		
	}

	/**
	 * 完成对view的初始化
	 */
	private void initView() {
		
		viewPager = (ViewPager) findViewById(R.id.content);

		homeLayout = findViewById(R.id.main_home_layout);
		historyLayout = findViewById(R.id.main_history_layout);
		settingLayout = findViewById(R.id.main_setting_layout);

		homeImage = (ImageView) findViewById(R.id.img_home);
		historyImage = (ImageView) findViewById(R.id.img_history);
		settingImage = (ImageView) findViewById(R.id.img_setting);

		homeLayout.setOnClickListener(this);
		historyLayout.setOnClickListener(this);
		settingLayout.setOnClickListener(this);

	}
	

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.main_home_layout:
			setTabSelection(0);
			viewPager.setCurrentItem(0);
			break;
		case R.id.main_history_layout:
			setTabSelection(1);
			viewPager.setCurrentItem(1);
			break;
		case R.id.main_setting_layout:
			setTabSelection(2);
			viewPager.setCurrentItem(2);
			break;
		default:
			break;
		}
	}
	
	private void setTabSelection(int index) {
		// 每次选中之前先清楚掉上次的选中状态
		clearSelection();

		switch (index) {
		case 0:
			homeImage.setImageResource(R.drawable.tab_weixin_pressed);
			break;
		case 1:
			historyImage.setImageResource(R.drawable.tab_address_pressed);
			break;
		case 2:
			settingImage.setImageResource(R.drawable.tab_find_frd_pressed);
			break;
		default:
			break;
		}
	}

	/**
	 * 清除掉所有的选中状态。
	 */
	private void clearSelection() {
		homeImage.setImageResource(R.drawable.tab_weixin_normal);
		historyImage.setImageResource(R.drawable.tab_address_normal);
		settingImage.setImageResource(R.drawable.tab_find_frd_normal);
	}

	/**
	 * 页面适配器
	 */
	public class TabPagerAdapter extends FragmentStatePagerAdapter {

		private List<android.support.v4.app.Fragment> list;

        TabPagerAdapter(android.support.v4.app.FragmentManager fm, ArrayList<android.support.v4.app.Fragment> list) {
            super(fm);
            this.list = list;
        }

		@Override
		public android.support.v4.app.Fragment getItem(int position) {
			return list.get(position);
		}

		@Override
		public int getCount() {
			return list.size();
		}
		
		
	}
		

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageSelected(int arg0) {
		setTabSelection(arg0);
	}

	public void exit_settings(View v) { // 退出 伪“对话框”，其实是一个activity
		Intent intent = new Intent(MainOrder.this, ExitFromSettings.class);
		startActivity(intent);
	}

	public void btn_shake(View v) { // 手机摇一摇
		Intent intent = new Intent(MainOrder.this, ShakeActivity.class);
		startActivity(intent);
	}



//	public void Btn_main_tab_btn1(View v) {
//		Toast.makeText(getApplicationContext(), "Btn1", Toast.LENGTH_SHORT)
//				.show();
//	}
//
//	public void Btn_main_tab_btn2(View v) {
//		Toast.makeText(getApplicationContext(), "Btn2", Toast.LENGTH_SHORT)
//				.show();
//	}
//
//	public void Btn_main_tab_btn3(View v) {
//		Toast.makeText(getApplicationContext(), "Btn3", Toast.LENGTH_SHORT)
//				.show();
//	}


}
